#include "maingame.h"

maingame::maingame(QWidget *parent) :  QMainWindow(parent)
{
    this->setFixedSize(1000,800);
    this->setWindowIcon(QPixmap(":/res/R-C.jpg"));//程序app图片
    this->setWindowTitle("对局进行中");//title

    games=new Gamestart;//创建第二个页面对象
    //退出按钮
    QPushButton* exitbtn=new QPushButton("退出对局",this);
    exitbtn->setFixedSize(100,40);
    exitbtn->setFont(QFont("SimHei",12,QFont::Bold));
    exitbtn->move(850,650);
    connect(exitbtn,QPushButton::clicked,[=](){
        int res=QMessageBox::question(this, "GAME OVER", "是否保存当前对局？", QMessageBox::Yes,QMessageBox::No);
        if(res==QMessageBox::Yes)
        emit game_back_continue();
        else
        {
            emit game_back();
        }
    });
    //悔棋按钮
    QPushButton* regretbtn=new QPushButton("悔棋",this);
    regretbtn->setFixedSize(100,40);
    regretbtn->setFont(QFont("SimHei",12,QFont::Bold));
    regretbtn->move(850,580);
    connect(regretbtn,QPushButton::clicked,[=](){
       this->regret_chess();
        update();
    });
}
bool maingame::winorn(int i,int j)
{
    int n=0;
    int k,l;
    k=i;
    l=j;
    //检测横向
    while (k>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        n++;
    }
    k=i+1;
    while (k<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        n++;
    }
    if(n>=5)
        return true;
    //检测纵向
    k=i;
    n=0;
    while (l>-1&&games->chess[k][l]==games->chess[i][j]) {
        l--;
        n++;
    }
    l=j+1;
    while (l<16&&games->chess[k][l]==games->chess[i][j]) {
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测左上左下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l--;
        n++;
    }
    k=i+1;
    l=j+1;
    while (k<16&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测右上右下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l++;
        n++;
    }
    k=i+1;
    l=j-1;
    while (k<16&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l--;
        n++;
    }
    if(n>=5)
        return true;


    return false;

}
void maingame::mousePressEvent(QMouseEvent *event)
{
    int x=event->x();
    int y=event->y();
    if(x>=this->width()*0.5-300&&x<=this->width()*0.5+300&&y>=this->height()*0.5-300&&y<=this->height()*0.5+300)
    {
       double m=(x-this->width()*0.5+300)/42.85;
       double n=(y-this->height()*0.5+300)/42.85;
       int i=(x-this->width()*0.5+300)/42.85;
       int j=(y-this->height()*0.5+300)/42.85;

       if(m-double(i)>=0.7)
           i++;
       else if(m-double(i)<=0.3)
           i=i;
       else
           return;

       if(n-double(j)>=0.7)
           j++;
       else if(n-double(j)<=0.3)
           j=j;
       else
           return;

       this->x=i;
       this->y=j;
       this->temp=games->chess_status;

       if(games->chess_status)
       {
           if(games->chess[i][j]==0)
           {
             games->chess[i][j]=1;
             update();
           }
           else
               return;
       }
       else
       {
           if(games->chess[i][j]==0)
            {
               games->chess[i][j]=-1;

               update();
            }
           else
               return;
       }
       if(this->winorn(i,j))
              {
                  if(games->chess_status)
                  {
                      int res=QMessageBox::question(this, "GAME OVER", "黑棋获胜！是否再来一局？", QMessageBox::Yes,QMessageBox::No);
                      if(res==QMessageBox::Yes)
                      games->game_init();
                      else
                      {
                          emit game_back();
                      }
                  }
                  else
                  {
                      int res=QMessageBox::question(this, "GAME OVER", "白棋获胜！是否再来一局？", QMessageBox::Yes,QMessageBox::No);
                      if(res==QMessageBox::Yes)
                      games->game_init();
                      else
                      {
                          emit game_back();
                      }
                  }
              }

       else
            games->chess_status=!games->chess_status;


       }
    }

void maingame::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/9802.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/new/prefix1/res/19101659901796309955316845.jpg");
    pix=pix.scaled(650,650);
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,this->height()*0.5-pix.height()*0.5,pix);

    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(games->chess[i][j]==-1)
            {
                pix.load(":/res/white.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
            else if(games->chess[i][j]== 1)
            {
                pix.load(":/res/black.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
        }
    }
}
void maingame::regret_chess()
{
    games->chess[this->x][this->y]=0;
    games->chess_status=this->temp;
}
